<?php

namespace App\Http\Requests;

use App\Http\Requests\Request;

class UserInfoRequest extends Request
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * 用户资料修改及创建表单验证
     *
     * @return array
     */
    public function rules()
    {
        $data = Request::all();
//        dd($data);

        $baseEditRules = [
            'uid'            => 'required|numeric',
            'nickname'       => 'required',
            'realname'       => 'required',
            'username'       => 'required',
            'followIndustry' => 'required|array|max:3',
            'zone_id'        => 'required|numeric',
            'email'          => 'required|e_mail',
            'zipcode'        => 'sometimes|numeric',
            'status'         => 'required',
            'rePassword'     => 'sometimes|same:newPassword',
            'newPassword'    => 'sometimes|same:rePassword',
        ];

        $investEditRules = [
            'uid'              => 'required|numeric',
            'nickname'         => 'required',
            'realname'         => 'required',
            'username'         => 'required',
            'followIndustry'   => 'required|array|max:3',
            'zone_id'          => 'required|numeric',
            'email'            => 'required|e_mail',
            'zipcode'          => 'sometimes|numeric',
            'status'           => 'required',
            'introduction'     => 'required',
            'investIndustry'   => 'required|array|max:3',
            'investCase'       => 'required',
            'institution_name' => 'required',
            'job'              => 'required',
            'tel'              => 'sometimes',
            'office_address'   => 'required',
            'office_zipcode'   => 'sometimes|numeric',
            'recommend'        => 'required|numeric',
            'new_card'         => 'sometimes',
            'rePassword'       => 'sometimes|same:newPassword',
            'newPassword'      => 'sometimes|same:rePassword',
        ];

        $baseCreateRules = [
            'username'       => 'required|numeric|unique:user,username',
            'nickname'       => 'required|unique:user,nickname',
            'realname'       => 'required',
            'password'       => 'required',
            'rePassword'     => 'required|same:password',
            'gender'         => 'required|boolean',
            'email'          => 'required|e_mail',
            'followIndustry' => 'required|array|max:3',
            'zone_id'        => 'required|numeric',
//            'address'        => 'required',
            'zipcode'        => 'sometimes|numeric',
            'business_card'  => 'sometimes',
        ];

        $investCreateRules = [
            'username'         => 'required|numeric|unique:user,username',
            'nickname'         => 'required|unique:user,nickname',
            'realname'         => 'required',
            'password'         => 'required',
            'rePassword'       => 'required|same:password',
            'gender'           => 'required|boolean',
            'email'            => 'required|e_mail',
            'followIndustry'   => 'required|array|max:3',
            'zone_id'          => 'required|numeric',
//            'address'          => 'required',
            'zipcode'          => 'sometimes|numeric',
            'business_card'    => 'sometimes',
            'introduction'     => 'required',
            'investIndustry'   => 'required|array|max:3',
            'investCase'       => 'required',
            'institution_name' => 'required',
            'job'              => 'required',
            'tel'              => 'sometimes',
            'office_address'   => 'required',
            'office_zipcode'   => 'sometimes|numeric',
        ];
//        return array();

//        dd($data);
        if ($data['formType'] == 'baseEdit') {
            $data['newPassword'] != '' && $baseEditRules['newPassword'] = 'required|same:rePassword';

//            dd($baseEditRules);
            return $baseEditRules;
        } elseif ($data['formType'] == 'investEdit') {
            $data['newPassword'] != '' && $investEditRules['newPassword'] = 'required|same:rePassword';
            $investEditRules['fund_min'] = 'sometimes|numeric';
            $investEditRules['fund_max'] = 'sometimes|numeric|min:' . $data['fund_min'];

            return $investEditRules;
        } elseif ($data['formType'] == 'baseCreate') {
            return $baseCreateRules;
        } elseif ($data['formType'] == 'investCreate') {
            $investCreateRules['fund_min'] = 'sometimes|numeric';
            $investCreateRules['fund_max'] = 'sometimes|numeric|min:' . $data['fund_min'];

            return $investCreateRules;
        }
    }

    public function attributes()
    {
        $attributes = array(
            'uid'              => '会员id',
            'nickname'         => '会员昵称',
            'realname'         => '真实姓名',
            'username'         => '会员账号',
            'role'             => '会员角色',
            'status'           => '会员状态',
            'investIndustry'   => '主投方向',
            'followIndustry'   => '关注的行业',
            'address'          => '会员地址',
            'email'            => '会员邮箱',
            'zipcode'          => '邮编',
            'zone_id'          => '所在地区',
            'institution_name' => '机构名称',
            'job'              => '职务',
            'office_address'   => '机构地址',
            'tel'              => '联系电话',
            'office_zipcode'   => '机构邮编',
            'investCase'       => '投资经历',
            'recommend'        => '是否推荐',
            'business_card'    => '名片',
            'password'         => '密码',
            'rePassword'       => '重复密码',
            'newPassword'      => '新密码',
            'fund_min'         => '投资金额最小值',
            'fund_max'         => '投资金额最大值',
        );

//        return array();
        return $attributes;
    }

    public function messages()
    {
        $messages = [
            'required'           => ':attribute为必填选项',
            'numeric'            => ':attribute必须为数字',
            'e_mail'             => ':attribute必须为合乎规范的邮箱',
            'unique'             => ':attribute必须不能重复',
            'same'               => ':attribute和:other必须相同',
            'gender.boolean'     => ':attribute必须为男、女中的一种',
            'fund_max.min'       => ':attribute必须比投资金额最小值大',
            'investIndustry.max' => ':attribute不能超过三个行业',
            'followIndustry.max' => ':attribute不能超过三个行业',
        ];

        return $messages;
    }
}
